package org.dftproject.lineagelinkage.ui.pages;

import org.dftproject.genesis.data.genealogy.Sex;
import org.dftproject.genesis.ui.SharedImages;
import org.dftproject.genesis.ui.figures.dualtree.ISelectableFigure;
import org.dftproject.genesis.ui.pages.pedigree.tree.FixedWidthColumnLayout;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

public class PersonFigure extends Figure implements ISelectableFigure {
	
	private final Font boldFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);

	public static final Color primaryBackgroundColor = new Color(null, 0xDD, 0xDD, 0xFF);
	public static final Color primaryBackgroundSelectedColor = new Color(null, 0xBB, 0xBB, 0xFF);
	public static final Color duplicateBackgroundColor = new Color(null, 0xDD, 0xFF, 0xDD);
	public static final Color duplicateBackgroundSelectedColor = new Color(null, 0xBB, 0xFF, 0xBB);
	public static final Color borderColor = new Color(null, 0x88, 0x88, 0x88);
	public static final Color borderSelectedColor = new Color(null, 0x66, 0x66, 0x66);
	public static final Color textColor = ColorConstants.black;
	public static final Color textSelectedColor = ColorConstants.black;
	
	private final Dimension corner = new Dimension(8, 8);

	private final boolean duplicate;
	private boolean selected = false;
	
	public PersonFigure(PersonData person, boolean duplicate) {
		this.duplicate = duplicate;
		
		setBorder(new MarginBorder(4, 4, 4, 6));
		setLayoutManager(new FixedWidthColumnLayout(200, 2));
		setForegroundColor(textColor);
		
		Figure container = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(2);
		layout.setStretchMinorAxis(true);
		container.setLayoutManager(layout);
		add(container);
		
		Label label = new Label();
		label.setLabelAlignment(PositionConstants.LEFT);
		label.setFont(boldFont);
		container.add(label);
		
		if (person.getName() == null)
			label.setText("(unknown)");
		else
			label.setText(person.getName());

		if (person.getSex() == Sex.Male)
			label.setIcon(SharedImages.getDefault().getImage(SharedImages.IMG_MALE));
		else if (person.getSex() == Sex.Female)
			label.setIcon(SharedImages.getDefault().getImage(SharedImages.IMG_FEMALE));
		else
			label.setIcon(SharedImages.getDefault().getImage(SharedImages.IMG_NULL));
		
		for (EventData event : person.getEvents()) {
			String text = event.getType();
			if (event.getDate() != null)
				text += " " + event.getDate();
			
			label = new Label(text);
			label.setLabelAlignment(PositionConstants.LEFT);
			container.add(label);
		}

		addMouseListener(new MouseListener.Stub() {

			@Override
			public void mousePressed(MouseEvent me) {
				me.consume();
				
				setSelected(true);
			}

		});
	}

	@Override
	public void paintFigure(Graphics graphics) {
		super.paintFigure(graphics);
		
		Rectangle bounds = getBounds();

		graphics.setBackgroundColor(selected ? (duplicate ? duplicateBackgroundSelectedColor : primaryBackgroundSelectedColor) : (duplicate ? duplicateBackgroundColor : primaryBackgroundColor));
		graphics.setAntialias(SWT.OFF);
		graphics.fillRoundRectangle(bounds, corner.width, corner.height);
	}
	
	@Override
	protected void paintBorder(Graphics graphics) {
		super.paintBorder(graphics);
		
		graphics.setBackgroundColor(selected ? borderSelectedColor : borderColor);
		graphics.setAntialias(SWT.ON);
		graphics.drawRoundRectangle(bounds.getResized(-1, -1), corner.width, corner.height);
	}

	public void setSelected(boolean selected) {
		if (this.selected == selected)
			return;

		this.firePropertyChange(ISelectableFigure.SELECTION_PROPERTY, this.selected, selected);
		this.selected = selected;
		
		setForegroundColor(selected ? textSelectedColor : textColor);

		repaint();
	}

	public boolean isSelected() {
		return selected;
	}

}
